package com.woniuxy.service.impl;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.dto.DictionaryDTO;
import com.woniuxy.entity.dto.operations.CardAddDTO;
import com.woniuxy.entity.dto.operations.CardDetailDTO;
import com.woniuxy.entity.dto.operations.CardDisplayDTO;
import com.woniuxy.entity.dto.operations.ClientDetailDTO;
import com.woniuxy.entity.po.operations.Card;
import com.woniuxy.mapper.CardMapper;
import com.woniuxy.service.CardService;
import com.woniuxy.util.*;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @BelongsProject: charging-management-platform
 * @BelongsPackage: com.woniuxy.service
 * @Author: Quan
 * @Date: 2025/3/28 11:59
 */
@Service
@AllArgsConstructor
public class CardServiceImpl implements CardService {
	private CardMapper cardMapper;
	private PageHelperUtil pageHelperUtil;
	private RedisTemplate<String, Object> redisTemplate;
	private final String REDIS_KEY_CONST = RedisKeyConst.DICTIONARY_KEY+ "MEMBERSHIPLEVEL";

	@Override
	public ResponseData<?> queryAll(PageParam<CardDisplayDTO> pageParam) {

		PageInfo<?> pageInfo = pageHelperUtil.getPageInfo(pageParam, cardMapper::queryAll, CardDisplayDTO::new);


//		ListValueConvertUtils.listConvert(cardMapper::queryAll, CardDisplayDTO::new);

//		PageInfo<?> pageInfo = PageHelperUtil.getPageInfo(pageParam, cardMapper::queryAll);

		return ResponseData.ok(pageInfo);
	}

	/**
	 * @Description: 查询所有vip类型
	 * @Param: []
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/3/29
	 * 1、先从redis中查询是否有对应的数据
	 * 		有：直接从redis中取
	 * 		无：从mapper中查询后存入redis中
	 */
	@Override
	public ResponseData<?> queryAllMembershipLevel() {
		HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
		if (hash.size(REDIS_KEY_CONST) != 0) {
			Map<Object, Object> entries = hash.entries(REDIS_KEY_CONST);
			List<DictionaryDTO> collect = entries.entrySet().stream().map(objectObjectEntry -> {
				DictionaryDTO dictionaryDTO = new DictionaryDTO();
				dictionaryDTO.setId(Long.parseLong((String) objectObjectEntry.getKey()));
				dictionaryDTO.setTypeValue((String) objectObjectEntry.getValue());
				return dictionaryDTO;
			}).collect(Collectors.toList());

			return ResponseData.ok(collect);
		}
		List<DictionaryDTO> dictionaryDTOS = ListValueConvertUtils.listConvert(cardMapper::queryAllMembershipLevel, DictionaryDTO::new);
		dictionaryDTOS.stream().forEach(dictionaryDTO -> {
			hash.put(REDIS_KEY_CONST, dictionaryDTO.getId().toString(), dictionaryDTO.getTypeValue());
		});
		redisTemplate.expire(REDIS_KEY_CONST, 20, TimeUnit.DAYS);
		return ResponseData.ok(dictionaryDTOS);
	}

	/**
	 * @Description: 通过id删除对应的卡
	 * @Param: [id]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/3/30
	 */
	@Override
	public ResponseData<?> delCardById(Long id) {
		cardMapper.delCardById(id);
		return ResponseData.ok();
	}

	/**
	 * @Description: 通过id禁用对应的卡
	 * @Param: [id]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/3/30
	 */
	@Override
	public ResponseData<?> forbiddenById(Long id) {
		cardMapper.forbiddenById(id);
		return ResponseData.ok();
	}

	/**
	 * @Description: 存放充电卡
	 * @Param: [cardAddDTO]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/3/31
	 */
	@Override
	public ResponseData<?> addCard(CardAddDTO cardAddDTO) {
		// 判断缓存中是否有vip类型
		HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
		if (hash.size(REDIS_KEY_CONST) == 0) {
			List<DictionaryDTO> dictionaryDTOS = ListValueConvertUtils.listConvert(cardMapper::queryAllMembershipLevel, DictionaryDTO::new);
			dictionaryDTOS.stream().forEach(dictionaryDTO -> {
				hash.put(REDIS_KEY_CONST, dictionaryDTO.getId(), dictionaryDTO.getTypeValue());
			});
			redisTemplate.expire(REDIS_KEY_CONST, 20, TimeUnit.DAYS);
		}
		// 从redis中取出对应的vip的id和value
		String membershipLevelValue = (String) hash.get(REDIS_KEY_CONST, cardAddDTO.getMembershipLevelId().toString());
		cardAddDTO.setMembershipLevelId(cardAddDTO.getMembershipLevelId());
		cardAddDTO.setMembershipLevelValue(membershipLevelValue);
		// 将card存放再数据库中

		cardMapper.addCard(cardAddDTO);
		return ResponseData.ok();
	}

	/**
	 * @Description: 通过id查对应的用户
	 * @Param: [id]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/3/31
	 */
	@Override
	public ResponseData<?> getCardById(Long id) {
		Card card = cardMapper.queryCardById(id);
		CardDetailDTO cardDisplayDTO = new CardDetailDTO(card);
		return ResponseData.ok(cardDisplayDTO);
	}

	/**
	 * @Description: 通过id对card进行批量删除
	 * @Param: [cardDisplayDTOS]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/3/31
	 */
	@Override
	public ResponseData<?> BatchDelCard(List<CardDisplayDTO> cardDisplayDTOS) {
		List<Long> ids = cardDisplayDTOS.stream().map(
				CardDisplayDTO::getId
		).collect(Collectors.toList());
		cardMapper.batchDelCardById(ids);
		return ResponseData.ok();
	}



}
